package com.extension.company.presenter.main;

import com.extension.company.common.mvp.BasePresenter;
import com.extension.company.contract.cart.FragmentCartContract;
import com.extension.company.model.entity.CarShopBean;
import com.extension.company.model.entity.ShopCarData;
import com.extension.company.model.main.FragmentCartMode;

import java.util.ArrayList;
import java.util.List;

/**
 * 作者：zwk
 * 时间：on 2019/3/27 15:54
 * 功能：购物车的p
 */
public class FragmentCartPresenter extends BasePresenter<FragmentCartMode, FragmentCartContract.View> {
	@Override
	public FragmentCartMode createModel() {
		return new FragmentCartMode();
	}

	@Override
	protected void onViewDestroy() {

	}

	/**
	 * 购物车支付
	 *
	 * @param goodsInfo 商品的信息
	 */
	public void payForCar(String goodsInfo) {
		model.carPayment(goodsInfo);
	}

	/**
	 * 获取购物车的列表
	 */
	public void getShopList() {
		model.carShopList(new FragmentCartContract.ShopListCallBack() {
			@Override
			public void shopCarM2P(List<CarShopBean.DataBean> shopList) { // 请求到的购物车数据回调
				List<ShopCarData> shopCarDataList = new ArrayList<>();
				for (int i = 0; i < shopList.size(); i++) {
					int id = shopList.get(i).getId();
					int userId = shopList.get(i).getUser_id();
					int busId = shopList.get(i).getBus_id();
					String storeName = shopList.get(i).getContent().get(0).getBus_name(); // 店铺的名称
					String storeImgPath = shopList.get(i).getContent().get(0).getLogo_image();
					ShopCarData storeData = new ShopCarData(); //组装数据
					storeData.setItemType(0); // 设置为店铺类型
					storeData.setStoreName(storeName); // 设置店铺的名称
					storeData.setStoreImg(storeImgPath);
					shopCarDataList.add(storeData); // 将店铺的数据设置到list中
					for (int j = 0; j < shopList.get(i).getContent().get(0).getGoods_info().size(); j++) {
						CarShopBean.DataBean.ContentBean.GoodsInfoBean goodsInfoBean = shopList.get(i).getContent().get(0).getGoods_info().get(j);
						ShopCarData shopCarData = new ShopCarData(); // 组装数据
						shopCarData.setCommodityNumber(String.valueOf(goodsInfoBean.getGoods_num()));// 设置商品的数量
						shopCarData.setCommodityName(goodsInfoBean.getGoods_name());
						shopCarData.setCommodityPrice(goodsInfoBean.getGoods_price());
						shopCarData.setItemType(1);
						shopCarData.setCommodityDetail(goodsInfoBean.getGoods_attr());
						shopCarData.setCommidityImg(goodsInfoBean.getSpec_image());
						shopCarData.setGoodsId(goodsInfoBean.getGoods_id());
						shopCarData.setGoodSkuId(goodsInfoBean.getSpec_sku_id());
						shopCarDataList.add(shopCarData); // 价格商品的数据也设置到数组中

					}
				}
				if (null != getView()) {
					getView().shopCarSuccess(shopCarDataList);
				}
			}
		});
	}
}
